package com.hx.vending.service.impl;

import com.hx.vending.bean.DataAudit;
import com.hx.vending.bean.User;
import com.hx.vending.dao.DataAuditMapper;
import com.hx.vending.dao.UserMapper;
import com.hx.vending.service.IDataAuditService;
import com.hx.vending.util.DateUtil;
import com.hx.vending.util.Msg;
import com.hx.vending.util.ToolUtil;
import org.apache.commons.collections4.CollectionUtils;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.List;

/**
 * Created by Administrator on 2019/2/2.
 */
@Service
public class DataAuditServiceImpl implements IDataAuditService {

    @Resource
    private UserMapper userMapper;
    @Resource
    private DataAuditMapper dataAuditMapper;

    @Override
    public Msg insertDataAudit(Integer userId) {
        if(ToolUtil.isEmpty(userId)){
            return Msg.fail().add("msg","用户不能为空");
        }
        User user = new User();
        user.setUserId(userId);
        User checkUser = this.userMapper.selectByPrimaryKey(user);

        if (checkUser == null) {

            return Msg.fail().add("msg", "用户不存在");
        }
        DataAudit dataAudit = new DataAudit();
        dataAudit.setUserId(userId);

        List<DataAudit> dataAuditList = this.dataAuditMapper.selectByExample(dataAudit);

        DataAudit newDataAudit = new DataAudit();

        newDataAudit.setUserId(userId);
        newDataAudit.setAuditCreatTime(DateUtil.getTime());
        newDataAudit.setAuditState(1);
        if (CollectionUtils.isEmpty(dataAuditList)) {

            this.dataAuditMapper.insertSelective(newDataAudit);

        } else {

            this.dataAuditMapper.updateAuditStateByUserId(newDataAudit);
        }
        return Msg.success();
    }

    @Override
    public int updateDataAuit(DataAudit dataAudit) {
        return dataAuditMapper.updateAuditStateByUserId(dataAudit);
    }

    @Override
    public List<DataAudit> selectByExample(DataAudit dataAudit) {
        return dataAuditMapper.selectByExample(dataAudit);
    }

    @Override
    public Integer selectCount(String userName, String state, String startTime, String endTime) {
        return dataAuditMapper.selectCount(userName,state,startTime,endTime);
    }
}
